<?php
namespace App\Http\Controllers;

use App\Models\Traveler;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

/**
 *
 * 我的出行人列表控制器
 *
 * @author maqu
 *
 */
class PeopleTravelController extends BaseController {

    //我的出行人 列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function lists($user_id){

        if(!$user_id){
            return $this->args_invalid();
        }

        //获取数据
        $address = Traveler::where('user_id',$user_id)->get();
        
        return $this->success($address->toArray());
	}
	
    //我的出行人 新增
    /**
     * @param $user_id
     * @return mixed
     */
	public function add($user_id){

        $input = Input::all();
        if(!$user_id){
            return $this->args_invalid();
        }
        $rules = array(
            'traveler_name'  => 'required|min:1|max:50',
            'is_child'  => 'required|min:1',
            'id_card'  => 'required|max:20',
            'card_type'=>'required',
            'phone_mob'=>'required|max:15|phone'
        );

	    $travelers = Validator::make($input, $rules);
    
        if ($travelers->fails()){
            return $this->failure($travelers->messages());
        }

        $traveler_name  = Input::get('traveler_name');
        $is_child       = Input::get('is_child');
        $id_card        = Input::get('id_card');
        $card_type = Input::get('card_type');
        $phone_mob = Input::get('phone_mob');

        $add_count = Traveler::where('user_id')->count();

//        if($add_count >config("MAX_TRAVELOR_NUM",10)){
//            return $this->failure('您最多可以添加'.config("MAX_TRAVELOR_NUM",10) .'个出行人');
//        }
        
        $data = array(
            'user_id'       => $user_id,
            'traveler_name'     => $traveler_name,
            'is_child'     => $is_child,
            'id_card'   => $id_card,
            'card_type'     => $card_type,
            'phone_mob'   => $phone_mob,
        );
        
        /* 添加出行人*/
        DB::table('travelers')->insert($data);

        return $this->success(null);
	}

    //我的出行人 修改
    /**
     * @param $user_id
     * @param $traveler_id
     * @return mixed
     * @throws Exception
     */
	public function edit($user_id,$traveler_id){

        if(!$user_id || !$traveler_id) {
            return $this->args_invalid();
        }

	    $input = Input::all();
        $rules = array(
            'traveler_name'  => 'required|min:1|max:50',
            'is_child'  => 'required|min:1',
            'id_card'  => 'required|max:20',
            'card_type'=>'required',
            'phone_mob'=>'required|max:15|phone'
        );

        $travelers = Validator::make($input, $rules);
        if ($travelers->fails()){
            return $this->failure($travelers->messages());
        }

        $traveler = Traveler::find($traveler_id);
        if(!$traveler){
            return $this->failure('该数据不存在');
        }

        if($traveler->user_id != $user_id){
            return $this->failure('你无权修改该数据');
        }

        $traveler_name  = Input::get('traveler_name');
        $is_child       = Input::get('is_child');
        $id_card        = Input::get('id_card');
        $card_type = Input::get('card_type');
        $phone_mob = Input::get('phone_mob');

        $traveler->traveler_name=$traveler_name;
        $traveler->is_child=$is_child;
        $traveler->id_card=$id_card;
        $traveler->card_type=$card_type;
        $traveler->phone_mob=$phone_mob;

        $traveler->save();

        return $this->success(null);
	}

    //我的出行列表 删除
    /**
     * @param $user_id
     * @param $traveler_id
     * @return mixed
     */
	public function delete($user_id,$traveler_id){

        if(!$user_id || !$traveler_id) {
            return $this->args_invalid();
        }

        $traveler = Traveler::find($traveler_id);
        if(!$traveler){
            return $this->failure('该数据不存在');
        }
        if($traveler->user_id != $user_id){
            return $this->failure('你无权修改该数据');
        }

        $traveler->delete();

        return $this->success(null);
	}

    public function bulk_delete($user_id){
        if(!$user_id) {
            return $this->args_invalid();
        }

        $ids = Input::get('ids');

        DB::table('travelers')
            ->where('user_id',$user_id)
            ->whereIn('traveler_id',$ids)
            ->delete();

        return $this->success(null);
    }

}
